using Godot;
using System;

public partial class MainMenu : CanvasLayer
{
	ScreenTransition screenTransition;
	PackedScene optionsScene = GD.Load<PackedScene>("res://scenes/ui/options_menu.tscn");
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		screenTransition = GetNode<ScreenTransition>("/root/ScreenTransition");
		GetNode<SoundButton>("%PlayButton").Pressed += OnPlayButtonPressed;
		GetNode<SoundButton>("%UpgradesButton").Pressed += OnUpgradesButtonPressed;
		GetNode<SoundButton>("%OptionsButton").Pressed += OnOptionsButtonPressed;
		GetNode<SoundButton>("%QuitButton").Pressed += OnQuitButtonPressed;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public async void OnPlayButtonPressed(){
		screenTransition.Transition();
		await ToSignal(screenTransition,nameof(screenTransition.TransitionedHalfway)); //等待信号结束
		GetTree().ChangeSceneToFile("res://scenes/main/main.tscn");
	}

	public async void OnUpgradesButtonPressed(){
		screenTransition.Transition();
		await ToSignal(screenTransition,nameof(screenTransition.TransitionedHalfway)); //等待信号结束
		GetTree().ChangeSceneToFile("res://scenes/ui/meta_menu.tscn");
	}

	public async void OnOptionsButtonPressed(){
		screenTransition.Transition();
		await ToSignal(screenTransition,nameof(screenTransition.TransitionedHalfway)); //等待信号结束
		GD.Print("OnOptionsButtonPressed等待完成");
		OptionsMenu optionsInstance = optionsScene.Instantiate() as OptionsMenu;
		AddChild(optionsInstance);
		optionsInstance.BackPressed += ()=>{this.OnBackPressed(optionsInstance);};
	}

	public void OnQuitButtonPressed(){
		GetTree().Quit();
	}

	public void OnBackPressed(OptionsMenu optionsInstance){
		optionsInstance.QueueFree();
	}
}
